<cfsetting enablecfoutputonly="Yes">

<!--- 
  /**
   * @author  <info@mireau.net>
   * @version 1.0 - <yyyy/mm/dd>
   */
--->

<cfsetting enablecfoutputonly="No">

<cfmodule template="#request.site.s_CFROOT#/lib/customtags/border.cfm">

<cfloop from="1" to="#arrayLen( request.fusebox.xFuseaction )#" index="nIndex">

  <cfscript>
    pre = request.fusebox.xFuseaction[nIndex][1];
    ext = request.fusebox.xFuseaction[nIndex][2];
    dir = request.fusebox.xFuseaction[nIndex][3];
  </cfscript>

  <!--- [rvl] Only proceed if we want this fusetype to be generated --->
  <cfif listFind( attributes.sType, pre )>
    <cfif fileExists( request.site.s_ANCHOR_PATH & "/lib/templates/system/#pre#.#ext#" )>
      <cffile 
        action="READ" 
        file="#request.site.s_ANCHOR_PATH#/lib/templates/system/#pre#.#ext#" 
        variable="local.sFileContents"
      >
    <cfelse>
      <cffile 
        action="READ" 
        file="#request.site.s_ANCHOR_PATH#/lib/templates/system/_default_.tpl" 
        variable="local.sFileContents"
      >
    </cfif>

    <cfscript>
      local.sDate = datePart( "yyyy", now()) & "/" &
                    numberFormat( datePart( "m", now()), "00" ) & "/" &
                    numberFormat( datePart( "d", now()), "00" );
                    
      local.sFileContents = replace( local.sFileContents, "%[EMAIL_ADDRESS]%",  "info@mireau.nl", "all" );
      local.sFileContents = replace( local.sFileContents, "%[DATE]%",           local.sDate, "all" );
      local.sFileContents = replace( local.sFileContents, "%[NEW_FUSEACTION]%", attributes.sNewFuse, "all" );
    </cfscript>
    
    <cfif not attributes.bFinalizeProcessing>
      <cfscript>
        local.sFullPath = "#request.site.s_ANCHOR_PATH#/www/#attributes.sDir#/_#dir#/#pre#_#lCase( attributes.sNewFuse )#.#ext#";
        local.nIndexCurrentDir = listFind( local.sFullPath, ".", "/" );
        
        if( local.nIndexCurrentDir neq 0 )
        {
          local.sFullPath = listDeleteAt( local.sFullPath, local.nIndexCurrentDir, "/" );
        }
      </cfscript>
      <fieldset>
        <legend><cfoutput>[#local.sFullPath#]</cfoutput></legend>
        
        <table border="0" cellpadding="0" cellspacing="0" width="100%">
          <tr>
            <td>
              <br/>
    
              <cfif not fileExists( "#request.site.s_ANCHOR_PATH#/www/#attributes.sDir#/_#dir#/#pre#_#lCase( attributes.sNewFuse )#.#ext#" )>
                <cfoutput><pre><span style="font-family: Courier New, Courier; background: ##f0f0f0; color: ##ff3300;">#HTMLEditFormat( local.sFileContents )#</span></pre></cfoutput>
              <cfelse>
                <span style="color: #ff0000; font-weight: bold;">file exists!</span>
                <br/>
                <br/>
              </cfif>
    
            </td>
          </tr>
        </table>
      </fieldset>
    <cfelse>
      <!--- [rvl] First: make sure we have a directory in the first place --->
      <cfif not directoryExists( "#request.site.s_ANCHOR_PATH#/www/#attributes.sDir#/_#dir#" )>
        <cfdirectory 
          action="create" 
          directory="#request.site.s_ANCHOR_PATH#/www/#attributes.sDir#/_#dir#"
        >
      </cfif>
      
      <!--- [rvl] Now, let's see about the file itself --->
      <cfif not fileExists( "#request.site.s_ANCHOR_PATH#/www/#attributes.sDir#/_#dir#/#pre#_#lCase( attributes.sNewFuse )#.#ext#" )>
        <cffile 
          action="write" 
          file="#request.site.s_ANCHOR_PATH#/www/#attributes.sDir#/_#dir#/#pre#_#lCase( attributes.sNewFuse )#.#ext#" 
          output="#local.sFileContents#" 
          addnewline="No"
        >
      </cfif>
    </cfif>
  </cfif>
</cfloop>

<cfif not attributes.bFinalizeProcessing>
  <form 
    action="<cfoutput>#request.site.s_WEBROOT#</cfoutput>/system/index.cfm" 
    method="post" 
    name="f" 
    id="f"
  >
    <!-- [rvl] hidden actions -->
    <input type="hidden" name="fuseaction"          value="addfuse">
    <input type="hidden" name="sDir"                value="<cfoutput>#attributes.sDir#</cfoutput>">
    <input type="hidden" name="sNewFuse"            value="<cfoutput>#attributes.sNewFuse#</cfoutput>">
    <input type="hidden" name="sType"               value="<cfoutput>#attributes.sType#</cfoutput>">
    <input type="hidden" name="bFinalizeProcessing" value="1">
    
  
    <fieldset>
      <legend>summary</legend>
  
      <br/>
  
      Right above here you'll find a summary of the actions that the system is 
      about to perform if you decide to proceed. Please, make sure you don't 
      make any mistakes before you proceed.
      <br/>
      <br/>
      Do you want to proceed?
      
      <hr/>
      
      <input 
        type="button" 
        value="back"
        onClick="history.back( -1 );"
      >
      
      <input
        type="submit" 
        value="proceed"
      >
    </fieldset>
  </form>
</cfif>

</cfmodule>